var agent = window.navigator.userAgent;
if (agent.indexOf('zhulong_CS') > -1) {
    document.write('<style>.clientHidden{display:none;}</style>');
}

function wordInit(prof) {
    function toJsonString(object) {
        var type = typeof object;
        if ('object' == type) {
            if (Array == object.constructor)
                type = 'array';
            else if (RegExp == object.constructor)
                type = 'regexp';
            else
                type = 'object';
        }
        switch (type) {
            case 'undefined' :
            case 'unknown' :
                return;
                break;
            case 'function' :
            case 'boolean' :
            case 'regexp' :
                return object.toString();
                break;
            case 'number' :
                return isFinite(object) ? object.toString() : 'null';
                break;
            case 'string' :
                return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case 'object' :
                if (object === null)
                    return 'null';
                var results = [];
                for (var property in object) {
                    var value = toJsonString(object[property]);
                    if (value !== undefined)
                        results.push(toJsonString(property) + ':' + value);
                }
                return '{' + results.join(',') + '}';
                break;
            case 'array' :
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = toJsonString(object[i]);
                    if (value !== undefined)
                        results.push(value);
                }
                return '[' + results.join(',') + ']';
                break;
        }
    };
    if (typeof prof == 'undefined') {
        proid = 0;
    } else {
        if (typeof prof == 'number') {
            proid = prof;
        } else {
            prof = prof.toLowerCase();
            var param = {
                'jz': 1,
                'sn': 2,
                'yl': 3,
                'zj': 4,
                'sg': 5,
                'ht': 6,
                'gp': 7,
                'dq': 8,
                'lq': 9,
                'gl': 10,
                'xm': 10,
                'jl': 11,
                'td': 12,
                'sl': 13,
                'aq': 14,
                'jg': 15,
                'yt': 22,
                'bim': 21,
                'qt': 16
            };
            proid = param[prof];
        }
        if (typeof proid == 'undefined') {
            proid = 0;
        }
    }
    var word = "";
    var cookiename = "searchKeywords_" + proid;
    var timestamp = Date.parse(new Date());
    var timename = "searchKeytime_" + proid;
    var cookieKeywords = null;
    if (localStorage) {
        var cookiename = "searchKeywords";
        var savetime = localStorage.getItem(timename);
        if ((timestamp - savetime) < 3600 * 24 * 7 * 1000) {
            var str = localStorage.getItem(cookiename);
            if (typeof str != "string" || str.indexOf(",") == -1) {
            } else {
                var dataResult = $.parseJSON(str);
                if (proid == 0 || typeof dataResult.per[proid] == "undefined") {
                    var cookieKeywords = dataResult.index.split(",");
                } else {
                    var cookieKeywords = dataResult.per[proid];
                }
            }
        }
    } else {
        cookieKeywords = $.cookie(cookiename);
    }
    if (cookieKeywords != null) {
        if (typeof cookieKeywords != "string") {
            var KeywordParam = cookieKeywords;
        } else {
            var KeywordParam = cookieKeywords.split(",");
        }
        var n = Math.floor(Math.random() * KeywordParam.length);
        var keywords = KeywordParam[n];
        word = keywords;
        $("#kuang").attr("placeholder", word);
    } else {
        $.ajaxSettings.async = false;
        $.getJSON("http://edu.zhulong.com/openapi/keyword/getKeywords", function (data) {
            $.ajaxSettings.async = true;
            if (data.errNo == 0) {
                var result = data.result;
                if (proid == 0 || typeof result.per[proid] == "undefined") {
                    var resultKeyword = result.index.split(",");
                } else {
                    var resultKeyword = result.per[proid];
                }
                var n = Math.floor(Math.random() * resultKeyword.length);
                var keywords = resultKeyword[n];
                if (localStorage) {
                    localStorage.setItem(timename, timestamp);
                    var str = toJsonString(data.result);
                    localStorage.setItem(cookiename, str);
                } else {
                    $.cookie(cookiename, resultKeyword, {expires: 3, path: '/', domain: '.zhulong.com'});
                }
                word = keywords;
                $("#kuang").attr("placeholder", word);
            }
        });
    }
    if (typeof word == "undefined") var word = "";
    return word;
};

function wordSearchlInit() {
    var word = "";
    //get and set user dropdownlist offset position
    var _rightOffset = ($('.u-name').width() + 15 - 106) * 0.5;
    $(".new_zhul_arrowxia").css("right", _rightOffset + 'px');
    word = wordInit(prof);
    if (low_IE) {
        $(".newzl_search_kuang").removeClass("newsearch_kuang2");
        $(".newzl_search_kuang").prepend('<img src="/img/pubimg/top_search_left.png" style="float: left;" alt="瑁呴グ绗﹀彿">');
        $(".newzl_search_kuang").append('<img src="/img/pubimg/top_search_right.png" style="float: left;" alt="瑁呴グ绗﹀彿">');
    }
    // low verison ie broswer
    $("#sou").click(function () {
        $("#kuang").animate({
            width: '250px',
            paddingLeft: '10px'
        });
        $(this).hide();
        $("#sousuo").show();
        $("#kuang").attr("placeholder", word);
        var value = $("input[name=q]").val();
        if (value == "") {
            /*alert("璇疯緭鍏ュ叧閿瘝");
            event.preventDefault();*/
            $("#kuang").val(word);
            $('#search form').submit();
            $("#kuang").val("");
        } else {
        }
    });
    $("#kuang").focus(function () {
        $("#kuang").css("color", "#444");
        $(this).animate({
            width: '250px'
        });
        $("#sousuo").show();
        $("#kuang").attr("placeholder", "璇疯緭鍏ュ叧閿瘝");

    })
    $(document).mouseup(function (e) {
        var _con = $(".newzl_search_kuang");
        if (!_con.is(e.target) && _con.has(e.target).length === 0) {
            $("#kuang").animate({
                width: '140px'
            });
            $("#sousuo").hide();
            $("#sou").show();
            $("#kuang").attr("placeholder", word);
        }
    });
    // search check
    $("#sousuo").click(function (event) {
        var value = $("input[name=q]").val();
        if (value == "") {
            /*alert("璇疯緭鍏ュ叧閿瘝");
            event.preventDefault();*/
            $("#kuang").val(word);
            $('#search form').submit();
            $("#kuang").val("");
        } else {
            $('#search form').submit();
        }
    });

    $("#clicksou").click(function (event) {
        var value = $("input[name=q]").val();
        if (value == "") {
            $("#kuang").val(word);
            $('#search form').submit();
            $("#kuang").val("");
        } else {
        }
    });


    /*
     * function moreshowHandle()
     */
    function moreshowHandle() {
        var show = 0;
        $("#more").mousemove(function () {
            show = 1;
            $("#detail").css("display", "block");
        });
        $("#more").mouseout(function (obj) {
            show = 0;
            setTimeout(function () {
                if (show == 0) {
                    $("#detail").css("display", "none");
                }
            }, 500);
        });
        $("#detail").mousemove(function () {
            show = 1;
            $("#detail").css("display", "block");
        });
        $("#detail").mouseout(function (obj) {
            show = 0;
            setTimeout(function () {
                if (show == 0) {
                    $("#detail").css("display", "none");
                }
            }, 500);
        });
    }

    moreshowHandle();
}

function formsub() {
    var value = $("input[name=q]").val();
    if (value == "") {
        $("#kuang").val(word);
        return true;
    } else {
        return true;
    }
}